import { mapGetters, mapMutations } from "vuex";

import { renderStr, verifyPc } from "$utils/index";
import { BASIC_FLAG } from "$utils/enum";
export default {
  data() {
    return {
      modalVislble: false,
      contentText: {
        contentrefresh: " "
      }
    };
  },
  created() {
    if (verifyPc()) {
      this.$nextTick(() => {
        window.addEventListener("keydown", this.keyupSubmit, false);
        this.$once("hook:beforeDestroy", () => {
          window.removeEventListener("keydown", this.keyupSubmit, false);
        });
      });
    }
  },
  computed: {
    displayContent() {
      console.log(this.useFlag, "useFlag");
      const titleStatus = BASIC_FLAG.getDescFromValue(this.useFlag)
      return renderStr(this.content, { ...this.selectRowData, titleStatus }) || ""
    },
    ...mapGetters(["modalLoading"]),
  },
  methods: {
    keyupSubmit(e) {
      if (this.visible && e.keyCode === 13) {
        this.submit()
      }
    },
    ...mapMutations({
      // 设置弹框标识
      setModalLoading: "app/SET_MODAL_LOADING"
    }),
    // 取消
    close() {
      this.$emit("close")
      this.modalVislble = false
      this.$emit("update:visible", false)
    },
    // 转换接口需要的参数
    transitionParams() {
      const params = {}
      Object.keys(this.params).forEach(key => {
        params[key] = this.params[key] ?? this.selectRowData[key] ?? ""
      })
      return params
    },
    // 确认
    async submit() {
      this.setModalLoading(true)
      // 获取当前使用的api
      const apiObj = this.apis.find(item => item.flag === this.useFlag)
      const params = this.transitionParams()
      await apiObj?.api(params)
      this.setModalLoading(false)
      this.close()
      this.$emit("submit")
      this.appMessage("操作成功", 0)
    },
    mModalClass() {
      return {
        opacity: this.modalVislble ? 1 : 0,
        zIndex: this.modalVislble ? 10072 : -100,
      }
    }
  }
}
